// pages/shareuser/shareuser.js
// <van-dialog id="van-dialog" />
import Dialog from '@vant/weapp/dialog/dialog';
var config = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {

    ShowAdduser: true,
    phone: "",
    user: "",
    uuid: "",
    userlist: []
  },

  onShowAddUser() {
    this.setData({
      ShowAdduser: !this.data.ShowAdduser
    })
  },
  onAddUserClick(e) {
    
    var _this = this
    console.log('onAddUserClick', e)
    console.log('detail', this.data.user)
    console.log('uuid', this.data.uuid)
    console.log('phone', this.data.phone)

    if (this.data.user.length < 11) {
      wx.showToast({
        title: '该用户格式不对',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.request({
      url: config.url.addOthderDev,
      method: 'POST',
      dataType: "json",
      data: {
        uuid: _this.data.uuid,
        phone: _this.data.user,
        mainphone: _this.data.phone,
        client: config.client,
      },
      header: {
        // 'content-type': 'application/json', // 默认值
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
      },
      success(res) {
        console.log(res.data)
        console.log("code:", res.data.code)
        let failMsgConfig = {
          201: '设备不存在',
          202: '主用户不存在',
          203: '该用户不存在',
          204: '不能添加相同用户',
          '205': '该用户已经存在',
        }
        if (res.data.code == 200) {
          _this.setData({
            user: "",
            // ShowAdduser:false
          })
          _this.getotherUser()
          wx.showToast({
            title: '添加成功',
            icon: 'succeed',
            duration: 2000
          })
        } else {
          let msg = failMsgConfig[res.data.code] || '添加失败，请检查'
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(e) {
        console.error(e)
      }
    })
  },

  onAddUseChange(e) {
    console.log('onAddUseChange', e)
    console.log('detail', e.detail)
    this.setData({
      user: e.detail
    })
  },

  onAddUseSearch(e) {
    console.log('onAddUseSearch', e)

  },

  getotherUser() {
    var _this = this
    wx.request({
      url: config.url.getOtherUser,
      method: 'POST',
      dataType: "json",
      data: {
        uuid: this.data.uuid,
        client: config.client,
      },
      header: {
        // 'content-type': 'application/json', // 默认值
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
      },
      success(res) {
        console.log(res.data)
        console.log("code:", res.data.code)
        console.log("length:", res.data.data.length)
        let isSuccess = res.data.code == 200
        let hasDataLenth = res.data.data && res.data.data.length == 0
        if (isSuccess) {
          _this.setData({
            userlist: res.data.data
          })
        }
        if (!isSuccess || hasDataLenth) {
          wx.showToast({
            title: '没有其他用户',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(e) {
        console.error(e)
      }
    })
  },

  onClick(index) {
    console.log(index)
    var _this = this
    var inx = index.currentTarget.dataset.bindex
    console.log(index.currentTarget.dataset.bindex, this.data.userlist[inx])

    Dialog.confirm({
        title: '删除用户',
        message: '确认是否删除该用户',
      })
      .then(() => {
        // on confirm
        wx.request({
          url: config.url.DelDev,
          method: 'POST',
          dataType: "json",
          data: {
            uuid: _this.data.uuid,
            phone: _this.data.userlist[inx],
            client: config.client,
          },
          header: {
            // 'content-type': 'application/json', // 默认值
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
          },
          success(res) {
            console.log(res.data)
            console.log("code:", res.data.code)
            if (res.data.code == 200) {
              wx.showToast({
                title: '删除成功',
                icon: 'succeed',
                duration: 2000
              })
              _this.getotherUser()
            } else {
              wx.showToast({
                title: '删除失败',
                icon: 'none',
                duration: 2000
              })
            }
          },
          fail(e) {
            console.error(e)
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    console.log("options:", options)
    console.log("uuid:", options.uuid)
    console.log("phone:", options.phone)
    this.setData({
      uuid: options.uuid,
      phone: options.phone
    })
    this.getotherUser()

  },
})